<script setup>
import { onMounted, ref } from 'vue'
import { getClazzCount,getChildSexCount } from '@/api/statistics'
import * as echarts from 'echarts'

const chartRef = ref(null)
const PieChart = ref(null)
// 图表数据
const option = ref({
      xAxis: {
        type: 'category',
        data: []
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          data: [],
          type: 'bar'
        }
      ]
    })
// 饼状图图表数据
const PieOption = ref({
  tooltip: {
    trigger: 'item'
  },
  legend: {
    top: '5%',
    left: 'center'
  },
  series: [
    {
      name: '',
      type: 'pie',
      radius: ['40%', '70%'],
      avoidLabelOverlap: false,
      itemStyle: {
        borderRadius: 10,
        borderColor: '#fff',
        borderWidth: 2
      },
      label: {
        show: false,
        position: 'center'
      },
      emphasis: {
        label: {
          show: true,
          fontSize: 40,
          fontWeight: 'bold'
        }
      },
      labelLine: {
        show: false
      },
      data: []
    }
  ]
});

// 初始化图表
onMounted(() => {
    getClazzCounts()
    getChildSexCounts()
  }
)

const getClazzCounts = async() => {
    await getClazzCount().then(res =>{
        const chartDom = chartRef.value
        const myChart = echarts.init(chartDom)
        option.value.xAxis.data = res.data.usernameList
        option.value.series[0].data = res.data.countList
        myChart.setOption({ ...option.value })
        console.log(res.data)
        console.log(option.value)
    })
}

const getChildSexCounts = async() => {
    await getChildSexCount().then(res =>{
        const chartDom = PieChart.value
        const myChart = echarts.init(chartDom)
        PieOption.value.series[0].data = res.data.childSexCountList
        PieOption.value.series[0].name = '男女总共：' + res.data.allCount
        console.log(res.data.childSexCountList)
        myChart.setOption({ ...PieOption.value })
    })
}
</script>

<template>
    <div class="flex">
        <div ref="chartRef" :style="{ width: '800px', height: '400px' }"></div>
        <div ref="PieChart" :style="{ width: '800px', height: '400px' }"></div>
    </div>
</template>

<style scoped>
.chart-container {
  width: 800px;
  height: 400px;
}
.flex{
    display: flex;
    justify-content: space-around;
}
</style>